package com.sharemarking.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sharemarking.system.entity.Permission;
import com.sharemarking.system.entity.Role;
import com.sharemarking.system.mapper.PermissionMapper;
import com.sharemarking.system.mapper.RoleMapper;

@Controller
@RequestMapping("system/role")
public class RoleController {
	
	@Autowired
	private RoleMapper rm;
	@Autowired
	private PermissionMapper pm;
	/**
	 * 角色管理列表页
	 * @param e
	 * @param model
	 */
	@GetMapping("index")
	@PreAuthorize("hasPermission('base','all')")
	public void index(Model model){
		List<Role> roleList=rm.getAll();
		model.addAttribute("list", roleList);
	}
	/**
	 * 角色新增页面
	 * @return
	 */
	@GetMapping("0")
	@PreAuthorize("hasPermission('base','all')")
	public String addPage(Model model){
		model.addAttribute("obj", new Role());
		List<Permission> list=pm.getAll();
		Set<String> domains=new HashSet<String>();
		for(Permission permission:list)domains.add(permission.getDomain());
		model.addAttribute("domains", domains);
		model.addAttribute("permissions", list);
		model.addAttribute("rolePermissionIds", new ArrayList<Integer>());
		return "system/role/form";
	}
	/**
	 * 新增与修改角色信息页面
	 * @param e
	 * @return
	 */
	@GetMapping("{id}")
	@PreAuthorize("hasPermission('base','all')")
	public String get(Role obj,Model model){
		Role target=rm.get(obj.getId());
		if(target==null)return "error/null";
		List<Permission> list=pm.getAll();
		Set<String> domains=new HashSet<String>();
		for(Permission permission:list)domains.add(permission.getDomain());
		model.addAttribute("obj",target);
		model.addAttribute("domains", domains);
		model.addAttribute("permissions", list);
		model.addAttribute("rolePermissionIds", rm.getPermissionIdsByRoleId(target.getId()));
		return "system/role/form";
	}
	/**
	 * 角色新增操作
	 * @param target
	 * @param model
	 * @return
	 */
	@PostMapping("0")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String insert(Role target,Model model,int... permission){
		if(target.getEnabled()==null)target.setEnabled(Boolean.FALSE);
		rm.insert(target);
		if(permission!=null)for(Integer permissionId:permission){
			rm.insertRolePermission(target.getId(), permissionId);
		}
		return "redirect:index";
	}
	/**
	 * 角色信息修改操作
	 * @param target
	 * @param model
	 * @return
	 */
	@PostMapping("{id}")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String update(Role target,int...permission){
		if(target.getEnabled()==null)target.setEnabled(Boolean.FALSE);
		List<Integer> list=rm.getPermissionIdsByRoleId(target.getId());
		rm.updateInfo(target);
		if(permission==null)permission=new int[0];
		for(int permissionId:permission){
			if(!list.contains(permissionId)){
				rm.insertRolePermission(target.getId(), permissionId);
			}
		}
		Arrays.stream(permission).forEach(i->{
			list.remove((Integer)i);
		});
		for(Integer permissionId:list){
			rm.deletePermission(target.getId(), permissionId);
		}
		return "redirect:index";
	}
	/**
	 * 删除指定角色
	 * @param id
	 */
	@GetMapping("{id}/delete")
	@PreAuthorize("hasPermission('base','all')")
	@Transactional
	public String delete(@PathVariable("id")Integer id){
		rm.deletePermissionByRoleId(id);
		rm.delete(id);
		return "redirect:../index";
	}
}
